﻿using OpenClientX.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace OpenClientX.Utils
{
    class CommontUtils
    {
        /// <summary>
        /// 检测是否有更新，比较当前版本号和数据库的版本号，你一样则说明有更新
        /// </summary>
        /// <param name="ServiceUrl"></param>
        /// <returns name="UpgradeTag">有更新返回true</returns>
        public static Boolean UpgradeTag(String ServiceUrl)
        {
            Boolean UpgradeTag = false;
            try
            {
                var versionv = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
                UpGradeModel versionModel = HttpUtil.getSysProperty("OpenClient_Version", ServiceUrl);
                //MessageBox.Show("获取版本号：" + versionModel.value+",当前版本号："+ versionv.ToString());
                if (versionv.ToString() != versionModel.value)
                {
                    UpgradeTag = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return UpgradeTag;
            }
            //MessageBox.Show("UpgradeTag:" + UpgradeTag);
            return UpgradeTag;
        }

        public static void DownloadUpgradeExe(String serviceUrl)
        {
            Dictionary<string, string> paramsDic = new Dictionary<string, string>();
            paramsDic.Add("account", "rr");
            paramsDic.Add("password", "rr");
            paramsDic.Add("key", "OpenClientX");
            HttpUtil.clientPost(serviceUrl + "/getActiveClient", paramsDic);

            Process p = Process.Start("C:/MssClientUpGrade/OpenClientX.msi");
        }

        /// <summary>
        /// 清除缓存的图像
        /// </summary>
        public static void clearCache()
        {
            #region
            String path = @"C:\MssClientCache\barcodeCache";     //扫描图像缓存
            try
            {
                DirectoryInfo dir = new DirectoryInfo(path);
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录

                foreach (FileSystemInfo i in fileinfo)
                {
                    if (i is DirectoryInfo)            //判断是否文件夹
                    {
                        DirectoryInfo subdir = new DirectoryInfo(i.FullName);
                        subdir.Delete(true);          //删除子目录和文件
                    }
                    else
                        File.Delete(i.FullName);      //删除指定文件
                }
            }
            catch (Exception ex)
            {
                return;
            }
            #endregion
        }
    }
}
